package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean){
       return roleService.findPage(pageBean);
    }

    @RequestMapping("/findAll")
    public Result findAll(){

        try {
            List<Role> roleList = roleService.findAll();
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    @RequestMapping("/addRole")
    public Result addRole(@RequestBody Role role, Integer[] menuIds, Integer[] permissionIds) {
        try {
            roleService.addRole(role, menuIds, permissionIds);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
    }


    //根据角色Id查询角色基础信息
    @RequestMapping("/findById")
    public Result findByRoleId(Integer roleId){
        try {
            Role role = roleService.findById(roleId);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    //根据角色Id查询对应的菜单Id数组和权限Id数组
    //数据结构Map集合存储两个Integer数组
    @RequestMapping("/findMenuIdsAndPermissionIdsByRoleId")
    public Result findMenuIdsAndPermissionIdsByRoleId(Integer roleId){
        try {
            Map<String, Integer[]> dataMap = roleService.findMenuIdsAndPermissionIdsByRoleId(roleId);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,dataMap);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    //修改保存
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role, Integer[] menuIds, Integer[] permissionIds){
        try {
            roleService.edit(role, menuIds, permissionIds);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }
    }

    //根据Id删除
    @RequestMapping("/deleteById")
    public Result deleteById(Integer roleId){
        try {
            roleService.deleteById(roleId);
            return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ROLE_FAIL);
        }
    }
}
